<?php
class BrandAction extends BaseAction
{
    public function index()
    {
    }

    /**
     * 供查询返回值
     * id,name,ename,initials,pic_url,small_pic_url,intr
     * @return bool|mixed|void
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function item()
    {
        $allFilelds = 'id,name,ename,initials,pic_url,small_pic_url,intr';
        $filelds = $this->getFields();
        //获取字段处理
        if ($filelds == '*') {
            $filelds = 'id, name, ename, initials, small_image';
        } else {
            $healthy = array("pic_url");
            $yummy = array("small_image");
            $filelds = str_replace($healthy, $yummy, $filelds);
        }
        $id = $this->_get('id', 'intval');
        $ename = $this->_get('ename');
        //获取
        $map = array();
        if (!empty($id)) {
            $map['id'] = $id;
        }
        if (!empty($ename)) {
            $map['ename'] = $ename;
        }
        if (!empty($id) || !empty($ename)) {
            $brand = M('Brand')->where($map)->field($filelds)->find();
            if (!empty($brand)) {
                //获取品牌其他信息
                //$brand['att_count'] = D('BrandAttention')->getAttCount($id);
                $brand['pic_url'] = D('Brand')->getBrandImage($brand['small_image']);
                unset($brand['small_image']);
            }
            $this->dataReturn($brand, 0, 1);
        } else {
            $this->dataReturn('获取数据失败', 0, 0);
        }
    }

    /**
     * 获取品牌列表 根据首字母获取品牌列表 分页数据
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function items()
    {
        $allFilelds = 'id,name,ename,initials,pic_url,small_pic_url,intr';
        $filelds = $this->getFields();
        //获取字段处理
        if ($filelds == '*') {
            $filelds = 'id, name, ename, initials';
        } else {
            $healthy = array("pic_url");
            $yummy = array("small_image");
            $filelds = str_replace($healthy, $yummy, $filelds);
        }
        $i = $this->_get('i');
        $p = $this->_get('p', 'intval');
        if (empty($p)) {
            $p = 1;
        }
        if (empty($i) || strlen($i) > 1) {
            $this->dataReturn('字母标识错误', 0, 0);
        } else {
            $BrandModel = D('Brand');
            $map = array();
            $map['initials'] = $i;
            $brandsCount = $BrandModel->where($map)->count();
            //执行分页处理
            $allPage = ceil($brandsCount / 9); //总页数
            if ($p > $allPage) {
                $this->dataReturn('分页码错误', 0, 0);
            }
            $firstRow = 9 * ($p - 1);
            //获取品牌数据
            $brands = $BrandModel->field($filelds)->where($map)->order('att_count')->limit($firstRow . ',9')->select();
            //获取品牌图片
            $data = array();
            $data['brands'] = $brands;
            $data['page'] = $p;
            $data['pagesize'] = $allPage;
            $this->dataReturn($data, 0, 1);
        }
    }

    /**
     * 品牌搜索
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function search()
    {
        $keyword = trim($this->_get('k'));
        $p = $this->_get('p', 'intval');
        if (empty($p)) {
            $p = 1;
        }
        if ($keyword) {
            $keyword = addslashes($keyword);
            if (empty($keyword)) {
            } else {
                $map = array();
                $map['ename'] = array('LIKE', '%' . strtolower($keyword) . '%');
                $map['name'] = array('LIKE', '%' . $keyword . '%');
                $map['_logic'] = 'OR';
                //执行分页处理
                $BrandModel = M('brand');
                $brandsCount = $BrandModel->where($map)->count();
                $allPage = ceil($brandsCount / 9); //总页数
                if ($p > $allPage) {
                    $this->dataReturn('分页码错误', 0, 0);
                }
                $firstRow = 9 * ($p - 1);
                $brands = $BrandModel->field('id,name, ename')
                    ->where($map)
                    ->order('ename')
                    ->limit($firstRow . ',9')
                    ->select();
                $data = array();
                $data['brands'] = $brands;
                $data['page'] = $p;
                $data['pagesize'] = $allPage;
                $this->dataReturn($data, 0, 1);
            }
        }
    }
}

?>